// pages/interface/interface.js
//获取应用实例
// pages/eventdetails/eventdetails.js
var util = require('../../utils/util.js');
var commonService = require('../../services/commonService.js');
var activityService = require('../../services/activityService.js');
var userGroupService = require('../../services/userGroupService.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
Page({
  data: {
    status: 3,
    activitys: [],
    activityHasMore: true,//默认还有更多活动列表
    activityPageIndex: 1,
    activityPageSize: 10,
    isManager:0,
  },
  liveClick: function (e) {
    var that = this;
    if (this.data.status === e.currentTarget.dataset.status) {
      return false;
    } else {
      that.setData({
        status: e.currentTarget.dataset.status,
        activityHasMore:true,
        activityPageIndex:1,
      })
      this.getJoinActivity();
    }
  },
  clickDetails: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../eventdetails/eventdetails?id=' + id + '&join=1'
    });
  },
  releaseActivity: function (e) {
    this.getJoinActivity(1);
  },
  onLoad: function () {
    var that = this;
    var today = new Date();
    this.getJoinActivity();
    this.initButton();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  initButton:function(){
    var that = this;
    userGroupService.isManager({
      success: function (resultData) {
        console.log(resultData);
        that.setData({
          isManager: resultData
        });
      }
    });
  },
  getJoinActivity: function (append) {
    var that = this;
    //是否继续加载，activityHasMore=false说明上一次查询之后没有获得更多数据了，因此不要再向服务器请求新的数据
    if (this.data.activityHasMore) {
      activityService.getActivityList({
        cityId: app.globalData.currentCityId,
        pageIndex: that.data.activityPageIndex,
        pageSize: that.data.activityPageSize,
        user_id: app.globalData.apiUser,
        status:that.data.status,
        success: function (resultData) {
          var activities = [];

          if (append == 1) {
            activities = that.data.activities;
          }

          for (var i = 0; i < resultData.list.length; i++) {
            var data = resultData.list[i];
            // date.replace(/-/g, "/")
            data.start_time = util.makeDateTimeToPretty(new Date(data.active_time_start.replace(/-/g, "/")), true);
            data.end_time = util.makeDateTimeToPretty(new Date(data.active_time_end.replace(/-/g, "/")), true);
            data.remainJoinNum = data.volunteer_num - data.join_num;
            activities.push(resultData.list[i]);
          }

          that.setData({
            activities: activities,
            activityPageIndex: that.data.activityPageIndex + 1,
            activityHasMore: resultData.hasMore,
          });
          console.log(that.data.activities)
        }
      });
    }
  },
  createActivity:function(){
    wx.navigateTo({
      url: '../manage/create'
    })
  },
  managerActivity:function(){
    wx.navigateTo({
      url: '../myActivityList/myActivityList'
    })
  },
})